<?php

namespace app\models\pay;

use Yii;

/**
 * This is the model class for table "pay".
 *
 * @property int $id
 * @property string $sn 订单传过来的SN, 如果订单SN修改,这里会重新创建一条记录
 * @property int $user_id
 * @property float $amount 支付金额
 * @property int $status 0未支付 1已支付
 * @property int $pay_type 支付方式 1 订单, 2 充值, 3 线下支付
 * @property string $pay_type_text 支付类型文本说明
 * @property string $pay_mode 支付方式, 微信
 * @property string $create_time
 * @property string $update_time
 * @property string $request_params
 */
class Pay extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'status', 'pay_type'], 'integer'],
            [['amount'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['sn', 'pay_type_text', 'pay_mode'], 'string', 'max' => 255],
            [['sn'], 'unique'],

            // json
            [['request_params'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'sn' => 'Sn',
            'user_id' => 'User ID',
            'amount' => 'Amount',
            'status' => 'Status',
            'pay_type' => 'Pay Type',
            'pay_type_text' => 'Pay Type Text',
            'pay_mode' => 'Pay Mode',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }
}
